package controller;


import dao.UserDao;
import dao.impl.UserDaoImpl;
import pojo.Label;
import pojo.Note;
import pojo.NoteLabelArray;
import pojo.Page;
import service.NoteLabelArrayService;
import service.PageService;
import service.impl.NoteLabelArrayServiceImpl;
import service.impl.PageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;


@WebServlet("/pageServlet")
public class PageServlet extends BaseServlet {
    /**
     * 所有笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String allNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        NoteLabelArrayService noteLabelArrayService = new NoteLabelArrayServiceImpl();
        ArrayList<NoteLabelArray> noteLabelArrays = new ArrayList<>();
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findPageNote(currentPage1, rows1, start);
        req.setAttribute("page", page);
        ArrayList<Note> noteList = page.getList();
        for (Note note : noteList) {
            ArrayList<Label> labels = noteLabelArrayService.labelArrayList(note.getNid());
            NoteLabelArray noteLabelArray = new NoteLabelArray();
            noteLabelArray.setLabelList(labels);
            noteLabelArray.setNid(note.getNid());
            noteLabelArrays.add(noteLabelArray);
        }
        req.setAttribute("noteLabelArrays", noteLabelArrays);


//        req.getRequestDispatcher("/allAreas.jsp").forward(req, resp);
        return "g_/allAreas.jsp";
    }

    /**
     * 区域笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String areaNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        String type = req.getParameter("type");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findAreaPageNote(type, currentPage1, rows1, start);

        req.setAttribute("page", page);

//        req.getRequestDispatcher("/AreaNotes.jsp").forward(req, resp);
        return "g_/AreaNotes.jsp";
    }

    /**
     * 用户查询个人笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String userNotePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        String usename = req.getParameter("usename");

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        Page page = new Page();
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        page = pageService.findUserPageNote(usename, currentPage1, rows1, start);

        req.setAttribute("page", page);
        req.setAttribute("usename", usename);

//        req.getRequestDispatcher("/usernotes.jsp").forward(req, resp);
        return "g_/usernotes.jsp";

    }

    /**
     * 用户查看审核笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String userCheckedNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        String usename = req.getParameter("usename");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findUserCheckedPageNote(usename, currentPage1, rows1, start);

        req.setAttribute("page", page);

//        req.getRequestDispatcher("/usercheckednote.jsp").forward(req, resp);
        return "g_/usercheckednote.jsp";
    }

    /**
     * 用户的个人区域笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String UserAreaNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        String type = req.getParameter("type");
        String usename = req.getParameter("usename");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findAreaPageNote(type, currentPage1, rows1, start);

        req.setAttribute("page", page);

//        req.getRequestDispatcher("/AreaNotes.jsp").forward(req, resp);
        return "g_/AreaNotes.jsp";
    }


    /**
     * 区域管理员区域笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String managerAreaNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        int mid = Integer.parseInt(req.getParameter("mid"));

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        UserDao userDao = new UserDaoImpl();
        String type = userDao.findAreaManager(mid);

        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findAreaPageNote(type, currentPage1, rows1, start);

        req.setAttribute("page", page);
        req.setAttribute("type", type);
        req.setAttribute("mid", mid);

//        req.getRequestDispatcher("/managerAreaNotes.jsp").forward(req, resp);
        return "g_/managerAreaNotes.jsp";
    }

    /**
     * manager查看审核笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String managerCheckedNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        String mid1 = req.getParameter("mid");
        int mid = Integer.parseInt(mid1);

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        UserDao userDao = new UserDaoImpl();
        String type = userDao.findAreaManager(mid);

        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findAreaCheckedPageNote(type, currentPage1, rows1, start);

        req.setAttribute("page", page);

//        req.getRequestDispatcher("/managerAreaCheckedNotes.jsp").forward(req, resp);
        return "g_/managerAreaCheckedNotes.jsp";
    }

    /**
     * 区域用户信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String areaUsersPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        int mid = Integer.parseInt(req.getParameter("mid"));
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        UserDao userDao = new UserDaoImpl();
        String type = userDao.findAreaManager(mid);

        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        Page page = new Page();
        PageService pageService = new PageServiceImpl();
        page = pageService.findAreaUserPage(type, currentPage1, rows1, start);

        req.setAttribute("page", page);
        req.setAttribute("mid", mid);

//        req.getRequestDispatcher("/areaUsers.jsp").forward(req, resp);
        return "g_/areaUsers.jsp";
    }

    /**
     * 系统管理员页面笔记
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String systemNotesPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {

        int mid = Integer.parseInt(req.getParameter("mid"));
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.findPageNote(currentPage1, rows1, start);

        req.setAttribute("mid", mid);
        req.setAttribute("page", page);

//        req.getRequestDispatcher("/systemNotes.html").forward(req, resp);
        return "g_/systemNotes.html";
    }

    /**
     * 搜索后分页
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String afterSearchPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        String content = req.getParameter("content");
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        NoteLabelArrayService noteLabelArrayService = new NoteLabelArrayServiceImpl();
        ArrayList<NoteLabelArray> noteLabelArrays = new ArrayList<>();
        //设置默认值
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.afterSearchPage(currentPage1, rows1, content, start);
        //System.out.println(page);
        req.setAttribute("page", page);
        //标签处理
        ArrayList<Note> noteList = page.getList();
        for (Note note : noteList) {
            ArrayList<Label> labels = noteLabelArrayService.labelArrayList(note.getNid());
            NoteLabelArray noteLabelArray = new NoteLabelArray();
            noteLabelArray.setLabelList(labels);
            noteLabelArray.setNid(note.getNid());
            noteLabelArrays.add(noteLabelArray);
        }
        //System.out.println(noteLabelArrays);
        //System.out.println(content);
        req.setAttribute("noteLabelArrays", noteLabelArrays);
        req.setAttribute("content", content);
//        req.getRequestDispatcher("/searchNotes.jsp").forward(req, resp);
        return "g_/searchNotes.jsp";
    }

    /**
     * 搜索后分页
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String afterSearchPageByFavourite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        String content = req.getParameter("content");

        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        NoteLabelArrayService noteLabelArrayService = new NoteLabelArrayServiceImpl();
        ArrayList<NoteLabelArray> noteLabelArrays = new ArrayList<>();
        //设置默认值
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.afterSearchPage(currentPage1, rows1, content, start);
        req.setAttribute("page", page);
        //标签处理
        ArrayList<Note> noteList = page.getList();
        for (Note note : noteList) {
            ArrayList<Label> labels = noteLabelArrayService.labelArrayList(note.getNid());
            NoteLabelArray noteLabelArray = new NoteLabelArray();
            noteLabelArray.setLabelList(labels);
            noteLabelArray.setNid(note.getNid());
            noteLabelArrays.add(noteLabelArray);
        }
        req.setAttribute("noteLabelArrays", noteLabelArrays);
        req.setAttribute("content", content);
//        req.getRequestDispatcher("/searchNotes.jsp").forward(req, resp);
        return "g_/searchNotes.jsp";
    }


    /**
     * 精确搜索后分页
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String afterExatSearchPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        String labelSearch = req.getParameter("labelSearch");
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        NoteLabelArrayService noteLabelArrayService = new NoteLabelArrayServiceImpl();
        ArrayList<NoteLabelArray> noteLabelArrays = new ArrayList<>();
        //设置默认值
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        int currentPage1 = Integer.parseInt(currentPage);
        int rows1 = Integer.parseInt(rows);
        int start = 0;
        start = (currentPage1 - 1) * rows1;
        PageService pageService = new PageServiceImpl();
        Page page = new Page();
        page = pageService.afterSearchPage(currentPage1, rows1, labelSearch, start);
        req.setAttribute("page", page);
        //标签处理
        ArrayList<Note> noteList = page.getList();
        for (Note note : noteList) {
            ArrayList<Label> labels = noteLabelArrayService.labelArrayList(note.getNid());
            NoteLabelArray noteLabelArray = new NoteLabelArray();
            noteLabelArray.setLabelList(labels);
            noteLabelArray.setNid(note.getNid());
            noteLabelArrays.add(noteLabelArray);
        }
        req.setAttribute("noteLabelArrays", noteLabelArrays);
        req.setAttribute("labelSearch", labelSearch);
//        req.getRequestDispatcher("/searchNotes.jsp").forward(req, resp);
        return "g_/searchNotes.jsp";
    }

}
